import java.io.File;
import java.io.IOException;

/**
 * @ClassName: Demo08
 * @Description: TODO File目录遍历与删除
 * @Author: MaykkStar@163.com
 */
public class Demo08 {

    public static void main(String[] args) throws IOException {
        newDirFile();
        final String SRC = ".//DelTest";
        File file = new File(SRC);
        printAll(file);
        delALl(file);
    }

    /**
     * 新建文件夹、文件
     */
    private static void newDirFile() throws IOException {
        for (int i = 0; i <= 5; i++) {
            String srcMK = ".//DelTest//Test";
            File mkdir = new File(srcMK + i);
            if (!mkdir.exists()) System.out.println("创建" + mkdir.getName() + mkdir.mkdirs());
            else System.out.println(mkdir.getName() + "已经存在了");
            File mkFile = new File(mkdir, "test" + i);
            System.out.println("srcMKnewFIle= " + mkFile.createNewFile());
        }
    }

    /**
     * 遍历文件夹中文件并输出
     * @param file 要遍历的文件夹
     */
    private static void printAll(File file) {
        File[] fileList = file.listFiles();
        if (null != fileList) {
            for (File f : fileList) {
                if (f.isDirectory()) {
//                    System.out.println("此处进入循环");
                    printAll(f);
                } else {
                    System.out.println(f.getName());
                }
            }
        }
    }

    /**
     * 遍历删除文件夹文件
     *
     * @param file 要删除的文件夹
     */
    private static void delALl(File file) {
        File[] fileList = file.listFiles();
        if (null != fileList) {
            for (File f : fileList) {
                if (f.isDirectory()) {
                    delALl(f);
                } else {
                    System.out.println("删除" + f.getAbsoluteFile() + f.delete());
                }
            }
        }
        System.out.println(file.delete());
    }
}
